﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.IO;

public partial class Controls_Activity_ActivityInvite : System.Web.UI.UserControl
{
    private User m_CurUser;
    private int m_ActivityId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request.QueryString["activityid"]))
        {
            Response.Redirect("~/home/default.aspx");
        }
        else
        {
            int.TryParse(Request.QueryString["activityid"], out m_ActivityId);
            bt_SendMail.Click += new EventHandler(bt_SendMail_Click);
            m_CurUser = UserRules.Instance.GetCurrentUser();
            txt_Name.Text = m_CurUser.FullName;
        }
    }

    void bt_SendMail_Click(object sender, EventArgs e)
    {
        Activity activity=ActivityRules.Instance.GetActivityInfo(m_ActivityId);
        string mailsubject = m_CurUser.FullName + "邀请你加入clubof.net网站中" + activity.Club.ClubName + "俱乐部的\"" + activity.ActivityName + "\"活动";
        FileStream fs = new FileStream(MapPathSecure("~/MailTemplate/ActivityInvite/ActivityInviteMailTemplate.txt"), FileMode.Open, FileAccess.Read);
        StreamReader m_streamReader = new StreamReader(fs);
        //使用StreamReader类来读取文件
        m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
        string str = m_streamReader.ReadToEnd();
        string mailcontent = string.Format(str, m_CurUser.FullName,activity.Club.ClubName + "俱乐部的\"" + activity.ActivityName + "\"活动",txt_InviteWords.Text, m_CurUser.UserId,m_ActivityId);
        //关闭此StreamReader对象
        m_streamReader.Close();
        
        string[] email = txt_Email.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        foreach (string em in email)
        {
            if (!string.IsNullOrEmpty(em.Trim()))
            {
                try
                {
                    ApiHelper.Instance.SendMail(em.Trim(), mailsubject, mailcontent, true);
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('已成功邀请好友加入活动！');", true);
                }
                catch
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('邮件发送失败！');", true);
                }
            }
        }
    }


}
